package com.qixin.modules.ea.controller;

import com.qixin.modules.ea.entity.EaPayment;
import com.qixin.modules.ea.entity.EaPhoneRecord;
import com.qixin.modules.ea.service.IEaPhoneRecordService;
import com.qixin.modules.ea.vo.EaPhoneRecordPage;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.ea.vo.PhoneRecordPageVo;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.IdVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.EmptyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
* @Description: 电话记录单
* @Author: qixin-boot
* @Date:   2021-07-06
* @Version: V1.0
*/
@Api(tags="app电话记录单")
@RestController
@RequestMapping("/app/api/eaPhoneRecord")
@Slf4j
public class ApiEaPhoneRecordController {
   @Autowired
   private IEaPhoneRecordService eaPhoneRecordService;

    /**
     * 查询电话记录单信息列表
     *
     * @param phoneRecordPageVo
     * @return Result
     */
    @ApiOperation(value="电话记录单信息列表-查询", notes="电话记录单信息列表-查询")
    @PostMapping(value = "/list")
    public Result<?> list(@RequestBody PhoneRecordPageVo phoneRecordPageVo) {
        Map<String,Object> resutlList = eaPhoneRecordService.list(phoneRecordPageVo);
        return Result.OK(resutlList);
    }

   /**
    *  编辑
    *
    * @param eaPhoneRecordPage
    * @return
    */
   @AutoLog(value = "电话记录单-编辑")
   @ApiOperation(value="电话记录单-编辑", notes="电话记录单-编辑")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody EaPhoneRecordPage eaPhoneRecordPage) {
       EaPhoneRecord eaPhoneRecord = new EaPhoneRecord();
       BeanUtils.copyProperties(eaPhoneRecordPage, eaPhoneRecord);
       EaPhoneRecord eaPhoneRecordEntity = eaPhoneRecordService.getById(eaPhoneRecord.getId());
       if(eaPhoneRecordEntity==null) {
           return Result.error("未找到对应数据");
       }

       //判断单据是否已经被提交
       EaPhoneRecord eaPhoneRecord1 = eaPhoneRecordService.getById(eaPhoneRecordPage.getId());
       String status = EmptyUtil.checkNull(eaPhoneRecord1.getBillStatus());   //当状态为1审批中,2已审批时,提示单据状态已变更,请刷新
       if(status.equals("1") || status.equals("2")){
           return Result.error("单据状态已变更,请刷新");
       }

       eaPhoneRecord.setSaveType("add");    //保存
       Map<String,Object> returnMap = eaPhoneRecordService.updateMain(eaPhoneRecord);

       if(returnMap.get("code").equals("-1")){
           return Result.error(EmptyUtil.checkNull(returnMap.get("message")));
       }else{
           returnMap.remove("code");
           returnMap.remove("message");
           return Result.OK(returnMap);
       }
   }

   /**
    *   通过id删除
    *
    * @param idVo
    * @return
    */
   @AutoLog(value = "电话记录单-通过id删除(未提交状态可以删除)")
   @ApiOperation(value="电话记录单-通过id删除", notes="电话记录单-通过id删除")
   @PostMapping(value = "/delete")
   public Result<?> delete(@RequestBody IdVo idVo) {
       String id = EmptyUtil.checkNull(idVo.getId());
       EaPhoneRecord eaPhoneRecordEntity = eaPhoneRecordService.getById(id);
       if(eaPhoneRecordEntity==null) {
           return Result.error("未找到对应数据");
       }
       Map<String,Object> returnMap = eaPhoneRecordService.delMain(id);
       if(returnMap.get("code").equals("-1")){
           String message = EmptyUtil.checkNull(returnMap.get("message"));
           return Result.error(message);
       }else{
           return Result.OK("删除成功!");
       }
   }

    /**
     * 通过id查询
     *
     * @param idVo
     * @return
     */
    @ApiOperation(value="电话记录单-通过id查询", notes="电话记录单-通过id查询")
    @PostMapping(value = "/queryById")
    public Result<?> queryById(@RequestBody IdVo idVo) {
        String id = EmptyUtil.checkNull(idVo.getId());
        EaPhoneRecord eaPhoneRecord = eaPhoneRecordService.getById(id);
        if(eaPhoneRecord==null) {
            return Result.error("未找到对应数据");
        }
        Map<String, Object> resutlList = eaPhoneRecordService.selectById(id);
        return Result.OK(resutlList);

    }

    /**
     * 电话记录单审批/退回
     *
     * @param eaTaskVo
     * @return
     */
    @AutoLog(value = "电话记录单审批/退回")
    @ApiOperation(value="电话记录单审批/退回", notes="电话记录单审批/退回")
    @PostMapping(value = "/audit")
    public Result<?> edit(@RequestBody EaTaskVo eaTaskVo) {
        Map<String,Object> returnMap = eaPhoneRecordService.audit(eaTaskVo);
        return Result.OK(returnMap);
    }

}
